<?php

namespace yiike\store\merchant\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yiike\store\merchant\models\OrderGoods;

/**
 * OrderGoodsSearch represents the model behind the search form about `yiike\store\merchant\models\OrderGoods`.
 */
class OrderGoodsSearch extends OrderGoods
{
    public $trade_id;
    public $is_pay;
    public $created_at;
    public $pay_at;
    public $status;
    public $is_refund;
    public $express_no;
    public $express_at;
    public $finished_at;


    public $status_option =[
        0 => ['' => '', 0 => '待付款', 1 => '待核销', 2 => '', 3 => '交易完成', 4 => '交易取消'],
        1 => ['' => '', 0 => '待付款', 1 => '已付款', 2 => '已发货', 3 => '交易完成', 4 => '交易取消'],
        2 => ['' => '', 0 => '待付款', 1 => '已付款', 2 => '已发货', 3 => '交易完成', 4 => '交易取消']
    ];

    public $ispay_option = ['' => '', 0 => '未付款', 1 => '已付款'];

    public $isrefund_option = ['' => '', 1 => '已退款'];

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'weid', 'store_id', 'order_id', 'goods_id', 'quan', 'give_credit1', 'num'], 'integer'],
            [['goods_sn', 'title', 'logo', 'spec', 'trade_id', 'is_pay', 'status', 'created_at', 'pay_at', 'is_refund', 'express_no', 'express_at', 'finished_at'], 'safe'],
            [['price', 'cost_price', 'commision', 'total'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
// bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'title' => '商品标题',
            'uid' => '用户id',
            'type' => '订单类型',
            'track_id' => '轨迹id',
            'date' => '日期',
            'trade_id' => '订单号',
            'uniontid' => '商户交易号',
            'nickname' => '昵称',
            'total' => '总金额',
            'quan' => '券抵扣',
            'commision' => '让利',
            'credit1' => '抵扣',
            'credit2' => '抵扣',
            'credit3' => '抵扣',
            'credit4' => '抵扣',
            'credit6' => '抵扣',
            'credit3_pay' => '',
            'real_pay' => '实付',
            'refund_amount' => '退款金额',
            'is_pay' => '付款状态',
            'is_refund' => '是否退款',
            'memo' => '备注',
            'recovery' => '回收站',
            'created_at' => '创建时间',
            'pay_at' => '支付时间',
            'province' => '省',
            'city' => '市',
            'district' => '区',
            'address' => '详细地址',
            'name' => '收货人',
            'mobile' => '手机号',
            'consumer_code' => '消费码',
            'status' => '订单状态',
            'express_name' => '物流公司',
            'express_code' => '物流公司代码',
            'express_no' => '快递单号',
            'express_at' => '发货时间',
            'finished_at' => '收货时间',
        ];
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = OrderGoods::find();
        $query->alias('t');
        $query->with('orderStore');
        $query->joinWith('orderStore orderStore');
        $query->joinWith('order order');

        if (Yii::$app->user->can('Admin') == false) {
            $this->store_id = Yii::$app->store->id;
        }

        if (! isset($params['sort'])) {
            $query->orderBy('t.id desc');
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'weid' => $this->weid,
            't.store_id' => $this->store_id,
            'order_id' => $this->order_id,
            'goods_id' => $this->goods_id,
            'price' => $this->price,
            'cost_price' => $this->cost_price,
            'quan' => $this->quan,
            'commision' => $this->commision,
            'give_credit1' => $this->give_credit1,
            'num' => $this->num,
            'total' => $this->total,
            'orderStore.status' => $this->status,
            'orderStore.is_pay' => $this->is_pay,
            'orderStore.is_refund' => $this->is_refund,
        ]);

        $query->andFilterWhere(['like', 'goods_sn', $this->goods_sn])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'logo', $this->logo])
            ->andFilterWhere(['like', 'spec', $this->spec])
            ->andFilterWhere(['like', 'orderStore.trade_id', $this->trade_id]);

        if ($this->created_at) {
            list ($start, $end) = explode(' - ', $this->created_at);
            $query->andFilterWhere(['between', 'order.created_at', strtotime($start), strtotime($end . ' 23:59:59')]);
        }

        if ($this->pay_at) {
            list ($start, $end) = explode(' - ', $this->pay_at);
            $query->andFilterWhere(['between', 'orderStore.pay_at', strtotime($start), strtotime($end . ' 23:59:59')]);
        }

        if ($this->express_at) {
            list ($start, $end) = explode(' - ', $this->express_at);
            $query->andFilterWhere(['between', 'orderStore.express_at', $start, $end . ' 23:59:59']);
        }

        return $dataProvider;
    }
}